from  random import choice
# ...function definitions...
def create_tunnel(cave_from,cave_to):
    """create a tunnel betweet cave_from and cave_to"""
    caves[cave_from].append(cave_to)
    caves[cave_to].append(cave_from)

def visit_cave(cave_number):
    """mark a cave as visited"""
    visited_caves.append(cave_number)
    unvisited_caves.remove(cave_number)

def choose_cave(cave_list):         #选择洞穴
    """pick a cave from a list ,provided that the cave has less than 3 tunnels"""
    cave_number=choice(cave_list)
    while len(cave_number)>=3:
        cave_number=choice(cave_list)
    return cave_number

def print_caves():
    """print out the current caves structure"""
    for number in cave_numbers:
        print number,":",caves[number]


def setup_caves(cave_numbers):
    """create the starting list of caves"""
    caves=[]
    for cave in cave_numbers:
        caves.append([])
    return caves

def link_caves():
    """make sure all of the caves are connected with tow-way tunnels"""
    while unvisited_caves !=[]:
        this_cave=choose_cave(visited_caves)
        next_cave=choose_cave(unvisited_caves)
    create_tunnel(this_cave,next_cave)
    visit_cave(next_cave)

def finish_caves():
    """link the rest of the caves with one-way tunnels"""
    for cave in cave_numbers:
        while len(caves[cave])<3:
            passage_to=choose_cave(cave_numbers)
            caves[cave].append(passage_to)

def print_location(play_location):
    """tell the player where they are"""
    print "you are in cave",play_location
    print "from here, you can see caves:"
    print caves[play_location]
    if wumpus_location in caves[play_location]:
        print "i smell a wumpus!"

def get_next_location():
    """get the player's next location"""
    print "which cave next?"
    player_input = raw_input(">")
    if (not player_input.isdigit() or int(player_input) not in caves[player_location]):
        print player_input + "?"
        print "that's not a drection that i can see!"
        return None
    else:
        return int(player_input)

cave_numbers=range(0,20)
unvisited_caves=range(0,20)
visited_caves=[]
caves=setup_caves(cave_numbers)

visit_cave(0)
print_caves()
link_caves()
print_caves()
finish_caves()

wumpus_location=choice(cave_numbers)
player_location=choice(cave_numbers)
while player_location=wumpus_location:
    player_location=choice(cave_numbers)
while True:
    print_location(player_location)
    new_location=get_next_location()
    if new_location is not None:
        player_location=new_location
    if player_location=wumpus_location:
        print "aargh! you got eaten by a wumpus!"
        break
